<?php

/**
 * This is the model class for table "category".
 *
 * The followings are the available columns in table 'category':
 * @property integer $cid
 * @property string $name
 * @property string $path
 * @property integer $view_order
 * @property integer $parent_dir
 *
 * The followings are the available model relations:
 * @property File[] $files
 * @property Link[] $links
 */
class Category extends CActiveRecord {

    /**
     * Returns the static model of the specified AR class.
     * @param string $className active record class name.
     * @return Category the static model class
     */
    public static function model($className=__CLASS__) {
        return parent::model($className);
    }

    /**
     * @return string the associated database table name
     */
    public function tableName() {
        return 'category';
    }

    /**
     * @return array validation rules for model attributes.
     */
    public function rules() {
        // NOTE: you should only define rules for those attributes that
        // will receive user inputs.
        return array(
            array('name, path', 'required'),
            array('view_order, parent_dir', 'numerical', 'integerOnly' => true),
            array('name', 'length', 'max' => 150),
            array('path', 'length', 'max' => 255),
            // The following rule is used by search().
            // Please remove those attributes that should not be searched.
            array('cid, name, path, view_order, parent_dir', 'safe', 'on' => 'search'),
        );
    }

    /**
     * @return array relational rules.
     */
    public function relations() {
        // NOTE: you may need to adjust the relation name and the related
        // class name for the relations automatically generated below.
        return array(
            'files' => array(self::HAS_MANY, 'File', 'cid'),
            'links' => array(self::HAS_MANY, 'Link', 'cid'),
        );
    }

    /**
     * @return array customized attribute labels (name=>label)
     */
    public function attributeLabels() {
        return array(
            'cid' => 'Cid',
            'name' => 'Name',
            'path' => 'Path',
            'view_order' => 'View Order',
            'parent_dir' => 'Parent Dir',
        );
    }

    /**
     * Retrieves a list of models based on the current search/filter conditions.
     * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
     */
    public function search() {
        // Warning: Please modify the following code to remove attributes that
        // should not be searched.

        $criteria = new CDbCriteria;

        $criteria->compare('cid', $this->cid);
        $criteria->compare('name', $this->name, true);
        $criteria->compare('path', $this->path, true);
        $criteria->compare('view_order', $this->view_order);
        $criteria->compare('parent_dir', $this->parent_dir);

        return new CActiveDataProvider($this, array(
                    'criteria' => $criteria,
                ));
    }

    public function getCategory($idc) {
        $connection = $this->initDB();
        $query = "SELECT cid, name, path, parent_dir FROM category WHERE cid='$cid'";
        $result = mysql_query($query);
        $this->closeDB($connection);
        return $result;
    }

    public function getCategoryName($idc) {
        $connection = $this->initDB();
        $query = "SELECT name FROM category WHERE cid='$cid'";
        $result = mysql_query($query);
        $row = mysql_fetch_array($result, MYSQL_NUM);
        $this->closeDB($connection);
        return $row[0];
    }

    public function getCategoryPath($idc) {
        $connection = $this->initDB();
        $query = "SELECT path FROM category WHERE cid='$cid'";
        $result = mysql_query($query);
        $row = mysql_fetch_array($result, MYSQL_NUM);
        $this->closeDB($connection);
        return $row[0];
    }

    public function getCategories($cid) {
        $connection=Yii::app()->db;        
        $query = "SELECT cid, name FROM category WHERE parent_dir='$cid'";
        $command = $connection->createCommand($query);
        $rows = $command->queryAll(); 
        //$result = mysql_query($query);
        //$this->closeDB($connection);
        return $rows;
    }

}